package vn.com.fis.portal.vtcc.service.persistence;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import vn.com.fis.portal.vtcc.model.SampleEntry;
import vn.com.fis.portal.vtcc.model.StrainEntry;
import vn.com.fis.portal.vtcc.model.impl.StrainEntryImpl;

import com.liferay.portal.kernel.dao.orm.QueryPos;
import com.liferay.portal.kernel.dao.orm.QueryUtil;
import com.liferay.portal.kernel.dao.orm.SQLQuery;
import com.liferay.portal.kernel.dao.orm.Session;
import com.liferay.portal.kernel.dao.orm.Type;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
import com.liferay.util.dao.orm.CustomSQLUtil;

public class StrainEntryFinderImpl extends BasePersistenceImpl<SampleEntry>
		implements StrainEntryFinder {
	private static Log _log = LogFactoryUtil.getLog(StrainEntryFinderImpl.class);
	
	final  String  FINDBYN_D_S = StrainEntryFinderImpl.class.getName() +".findByN_D_S";
	final  String  COUNTBYN_D_S = StrainEntryFinderImpl.class.getName() +".countByN_D_S";
	
	final  String  FINDBYN_D = StrainEntryFinderImpl.class.getName() +".findByN_D";
	final  String  COUNTBYN_D = StrainEntryFinderImpl.class.getName() +".countByN_D";
	
	/**
	 * @findByN_D_S
	 * @param strainName
	 * @param description
	 * @param status
	 * @andOperator boolean 
	 * @andOperator start
	 * @andOperator end
	 * @return List<StrainEntry>
	 * 
	 */
	public List<StrainEntry> findByN_D_S(String strainName ,String description ,int status, boolean andOperator , int start, int end) throws SystemException {
		Session session = null;
		List<StrainEntry> strainEntries = new ArrayList<StrainEntry>();
		try {
			session = openSession();
			String sql="";
			if(status>0){
				  sql = CustomSQLUtil.get(FINDBYN_D_S);
			}
			else{ 
				  sql = CustomSQLUtil.get(FINDBYN_D);
			}
			
			sql = CustomSQLUtil.replaceAndOperator(sql, andOperator); // AND
			SQLQuery query = session.createSQLQuery(sql);
			query.addEntity(StrainEntryImpl.TABLE_NAME, StrainEntryImpl.class);
			QueryPos qPos = QueryPos.getInstance(query);
			
			// add param 
			
			qPos.add("%"+strainName+"%");
			qPos.add(strainName);
			
			qPos.add("%"+description+"%");
			qPos.add(description);
			
			
			if(status>0){
				qPos.add("%"+status+"%");
				qPos.add(status);
			}
			 
			strainEntries = (List<StrainEntry>) QueryUtil.list(query, getDialect(), start,
					end);
			/*q.addScalar(COUNT_COLUMN_NAME, Type.LONG);

			Iterator<Long> itr = q.iterate();

			if (itr.hasNext()) {
				Long count = itr.next();

				if (count != null) {
					return count.intValue();
				}
			}*/

			/*Object  obj = query.uniqueResult();
			count = Integer.valueOf(obj.toString());
			*/
			
		}
		catch (Exception e) {
			//throw new SystemException(e);
			_log.error(e,e);
		}
		finally {
			closeSession(session);
		}
		return strainEntries;
	}
	
	/**
	 * @countByN_D_S
	 * @param strainName
	 * @param description
	 * @param status
	 * @andOperator boolean 
	 * @return int value
	 * 
	 */
	public int countByN_D_S(String strainName ,String description ,int status, boolean andOperator ) throws SystemException {
		Session session = null;
		int count=0;
		try {
			session = openSession();
			String sql="";
			if(status>0){
				  sql = CustomSQLUtil.get(COUNTBYN_D_S);
			}
			else{ 
				  sql = CustomSQLUtil.get(COUNTBYN_D);
			}
			
			sql = CustomSQLUtil.replaceAndOperator(sql, andOperator); // AND
			SQLQuery query = session.createSQLQuery(sql);
			QueryPos qPos = QueryPos.getInstance(query);
			
			// add param 
			
			qPos.add("%"+strainName+"%");
			qPos.add(strainName);
			
			qPos.add("%"+description+"%");
			qPos.add(description);
			
			
			if(status>0){
				qPos.add("%"+status+"%");
				qPos.add(status);
			}
			 
			Object  obj = query.uniqueResult();
			count = Integer.valueOf(obj.toString());
			
		}
		catch (Exception e) {
			//throw new SystemException(e);
			_log.error(e,e);
		}
		finally {
			closeSession(session);
		}
		return count;
	}
}